package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.dto.JsonReturn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * Created by zhenghao on 2016/10/10.
 *
 */
@Slf4j
@Controller
@RequestMapping("/admin/upload")
public class UploadController {

    @Value("${image.web.host}")
    private String imageHost;         //图片访问虚拟路径

    @Value("${image.save.path}")
    private String imageSavePath;    //抓拍图片保存路径


    @RequestMapping("/uploadFiles.do")
    @ResponseBody
    public JsonReturn uploadFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {

        log.info("--- in uploadFile");

        try {

            String imageUrl = "";

            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext());

            if (multipartResolver.isMultipart(request)) {

                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                MultipartFile file = multiRequest.getFile("upload_file");

                imageUrl = disposeFile(file);

                if(imageUrl == null) throw new Exception("图片上传失败，请检查系统日志");

            }

            return new JsonReturn(1,"success",imageUrl);


        } catch (Exception e) {

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());

        }

    }

    private String disposeFile(MultipartFile file) {

        //定义图片名称
        String imageName = String.valueOf(new Date().getTime()) + ".jpg";

        String imageFallName = imageSavePath + imageName;

        log.info("-----image save path :"+imageFallName);


        BufferedOutputStream stream = null;

        try {

            //按新的文件名称写入文件
            byte[] bytes = file.getBytes();

            stream = new BufferedOutputStream(new FileOutputStream(new File(imageFallName)));

            stream.write(bytes);

            stream.close();

            //访问文件虚拟访问路径
            return imageHost + imageName;

        } catch (IOException e) {
            e.printStackTrace();

        }finally {

            if(stream!=null){

                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return null;
    }
}
